package cn.itsource.pethome.pet.controller;

import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.query.SearchMasterMessageQuery;
import cn.itsource.pethome.pet.service.ISearchMasterMessageService;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import cn.itsource.pethome.utils.RedisUtils;
import cn.itsource.pethome.utils.UserContext;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/mmc")
public class SearchMasterMessageController {

    @Autowired
    private ISearchMasterMessageService searchMasterMessageService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMessage searchMasterMessage,HttpServletRequest request){
        //SearchMasterMessage(name=1, price=1.0, age=1, gender=true, coatColor=1,
        // resources=null, petType=null, address=武汉市洪山区武汉火车站-地铁站, state=null, title=1, user=null, shop=null)
        //组装数据
        //用户发布寻主信息，将该用户的信息组装到对象中
        User user = UserContext.getUser(request);
        searchMasterMessage.setUser(user);
        AjaxResult result = searchMasterMessageService.publish(searchMasterMessage);
        return result;
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        searchMasterMessageService.delete(id);
        return AjaxResult.me();
    }

    @PostMapping
    public PageResult<SearchMasterMessage> seleceByPage(@RequestBody SearchMasterMessageQuery query){
        return searchMasterMessageService.selectByPage(query);
    }

    @PatchMapping("/batchProcessing")
    public AjaxResult batchProcessing(@RequestBody Long[] ids){
        return searchMasterMessageService.batchProcessing(ids);
    }
}
